
	use "Data/Raw/baseline_merged.dta", clear


		
* Generate variables

	* Clean party ID
	
		gen partyID_baseline=0 if inparty=="Democrat"
			replace partyID_baseline=1 if inparty=="Republican"
		
			cap la def partyID_baseline 0 "Democrat" 1 "Republican"
			la val partyID_baseline partyID_baseline
			la var partyID_baseline "Baseline: Party ID"
				
		gen partyID_baseline_alt=0 if partisanship=="Democrat"
			replace partyID_baseline_alt=1 if partisanship=="Republican"
			replace partyID_baseline_alt=2 if partisanship=="Independent"
			
			replace partyID_baseline_alt=0 if partisanship_4_text=="Green Party"
			replace partyID_baseline_alt=0 if partisanship_4_text=="Green Zionist"			
			replace partyID_baseline_alt=0 if partisanship_4_text=="Moderate Democrat"			
			replace partyID_baseline_alt=0 if partisanship_4_text=="Social democrat"			
			replace partyID_baseline_alt=0 if partisanship_4_text=="progressive"			
				
			replace partyID_baseline_alt=1 if partisanship_4_text=="Conservative by temperament, but unhappy with both parties, & the way issues are cropped and framed"
			replace partyID_baseline_alt=1 if partisanship_4_text=="Constituional conservative favoring balanced budgets"
			replace partyID_baseline_alt=1 if partisanship_4_text=="Libertarian "
				
			replace partyID_baseline_alt=2 if partisanship_4_text=="Anarchist"
			replace partyID_baseline_alt=2 if partisanship_4_text=="Centrist"
			replace partyID_baseline_alt=2 if partisanship_4_text=="Permanent Resident Taxpayer"
				
			replace partyID_baseline_alt=2 if partisanship_4_text=="" & partisanship=="Other"
				
			cap la def partyID_baseline_alt 0 "Democrat" 1 "Republican" 2 "Independent"
			la val partyID_baseline_alt partyID_baseline_alt
			la var partyID_baseline_alt "Baseline: Party ID (with Independents)"
								
			gen partyID_endline1_D = (partyID_baseline_alt==0) if partyID_baseline_alt!=.
				la var partyID_endline1_D "Baseline: Democrat (with Independents)"
				
			gen partyID_endline1_R = (partyID_baseline_alt==1) if partyID_baseline_alt!=.
				la var partyID_endline1_R "Baseline: Republican (with Independents)"
				
			gen partyID_endline1_I = (partyID_baseline_alt==2) if partyID_baseline_alt!=.
				la var partyID_endline1_I "Baseline: Independent (with Independents)"
							
			gen partyID_merged_D = (partyID_merged==0) if partyID_merged!=.
			gen partyID_merged_R = (partyID_merged==1) if partyID_merged!=.
							
								
	* Generate inparty vs. outparty feeling thermometer
	
		gen therm_diff = therm_inparty-therm_outparty
			la var therm_diff "Baseline: Inparty vs. outparty feeling thermometer"
		
		gen therm_cool_inparty = 100-therm_inparty
			la var therm_cool_inparty "Baseline: Inparty coolness"

		gen therm_cool_outparty = 100-therm_outparty
			la var therm_cool_outparty "Baseline: Outparty coolness"

			
	* Generate negative partisanship dummy
		
		gen neg_partisan = (val_neg_part==1) if val_neg_part!=.
			la var neg_partisan "Baseline: Negative partisanship"
	
	
	
	* Generate inparty vs. outparty trust
	
		gen trust_inparty = 5 if q76=="Almost always"
			replace trust_inparty = 4 if q76=="Most of the time"
			replace trust_inparty = 3 if q76=="About half the time"
			replace trust_inparty = 2 if q76=="Once in a while"
			replace trust_inparty = 1 if q76=="Almost never"
	
		gen trust_outparty = 5 if q711=="Almost always"
			replace trust_outparty = 4 if q711=="Most of the time"
			replace trust_outparty = 3 if q711=="About half the time"
			replace trust_outparty = 2 if q711=="Once in a while"
			replace trust_outparty = 1 if q711=="Almost never"
		
		gen trust_diff = trust_inparty - trust_outparty
			la var trust_diff "Baseline: Inparty vs. outparty trust"
	
		gen distrust_inparty = 6-trust_inparty
			la var distrust_inparty "Baseline: Inparty distrust"

		gen distrust_outparty = 6-trust_outparty
			la var distrust_outparty "Baseline: Outparty distrust"
	
	* Generate comfort with outparty
	
		gen comfort_outparty_friends = 3 if q77=="Not comfortable"
			replace comfort_outparty_friends = 2 if q77=="Somewhat comfortable"
			replace comfort_outparty_friends = 1 if q77=="Very comfortable"
		
		gen comfort_outparty_neighbors = 3 if q78=="Not comfortable"
			replace comfort_outparty_neighbors = 2 if q78=="Somewhat comfortable"
			replace comfort_outparty_neighbors = 1 if q78=="Very comfortable"
		
		gen comfort_outparty_marriage = 3 if q79=="Not comfortable"
			replace comfort_outparty_marriage = 2 if q79=="Somewhat comfortable"
			replace comfort_outparty_marriage = 1 if q79=="Very comfortable"
	
	* Generate threat perceptions 
			
		gen threat_outparty_cit = (val_prop_cit==2) if val_prop_cit!=.
		gen threat_outparty_pol = (val_prop_pol==2) if val_prop_pol!=.
	
	* Generate affective polarization index
	
		alpha therm_diff neg_partisan trust_diff comfort_outparty_friends comfort_outparty_neighbors comfort_outparty_marriage threat_outparty_cit threat_outparty_pol, std item detail gen(aff_pol_idx)
			la var aff_pol_idx "Baseline: Attitudinal affective polarization index"
		
		alpha therm_cool_outparty neg_partisan distrust_outparty comfort_outparty_friends comfort_outparty_neighbors comfort_outparty_marriage threat_outparty_cit threat_outparty_pol, std item detail gen(aff_pol_idx_out)
			la var aff_pol_idx_out "Baseline: Attitudinal affective polarization index (outparty only)"

		alpha therm_diff trust_diff comfort_outparty_friends comfort_outparty_neighbors comfort_outparty_marriage threat_outparty_cit threat_outparty_pol, std item detail gen(aff_pol_idx_noneg)
			la var aff_pol_idx_noneg "Baseline: Attitudinal affective polarization index (no negative partisanship)"

		alpha therm_cool_outparty distrust_outparty comfort_outparty_friends comfort_outparty_neighbors comfort_outparty_marriage threat_outparty_cit threat_outparty_pol, std item detail gen(aff_pol_idx_noneg_out)
			la var aff_pol_idx_noneg_out "Baseline: Attitudinal affective polarization index (outparty only, no negative partisanship)"
			
	* Generate anti-democratic attitudes index
	
		foreach n of numlist 1/8 {
			gen antidem_dem`n' = 4 if qpd`n'=="Strongly support"
				replace antidem_dem`n' = 3 if qpd`n'=="Somewhat support"
				replace antidem_dem`n' = 2 if qpd`n'=="Somewhat oppose"
				replace antidem_dem`n' = 1 if qpd`n'=="Strongly oppose"
			gen antidem_rep`n' = 4 if qpr`n'=="Strongly support"
				replace antidem_rep`n' = 3 if qpr`n'=="Somewhat support"
				replace antidem_rep`n' = 2 if qpr`n'=="Somewhat oppose"
				replace antidem_rep`n' = 1 if qpr`n'=="Strongly oppose"
			gen antidem`n' = antidem_dem`n'
				replace antidem`n' = antidem_rep`n' if antidem`n'==.
		}
		
		alpha antidem1 antidem2 antidem3 antidem4 antidem6 antidem7 antidem8, std item detail gen(antidem_idx)

		
			
	* Generate wave indicator
	
		gen wave = 0
			
	* Redefine attriters as respondents who didn't complete any of the survey
	
		destring progress, replace
		replace attrition_baseline = 1 if progress==0

	* Redefine attriters as respondents for whom we don't have an affective polarization score
	
		replace attrition_baseline = 1 if aff_pol_idx_noneg==.
	
		gen non_attrition_baseline = 1-attrition_baseline
	


* Save
	
	save "Data/Cleaned/baseline_merged_cleaned.dta", replace
	
	
